﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MadeInPortugal.DataSet1TableAdapters;
using System.Collections;

namespace MadeInPortugal.Class
{
    public class Product
    {
        int id;
        string productName;
        Enterprise enterprise;
        decimal price;
        string specifications;
        int minReq;
        ProductImage image;
        Tag tag;
        ProdProperties[] properties;
        int priority;
        DateTime priorityEndDate;
        decimal priorityFinal;

        TBProductTableAdapter productTableAdapter = new TBProductTableAdapter();

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string ProductName
        {
            get { return productName; }
            set { productName = value; }
        }
        public Enterprise Enterprise
        {
            get { return enterprise; }
            set { enterprise = value; }
        }
        public Decimal Price
        {
            get { return price; }
            set { price = value; }
        }
        public string Specifications
        {
            get { return specifications; }
            set { specifications = value; }
        }
        public int MinReq
        {
            get { return minReq; }
            set { minReq = value; }
        }
        public ProductImage Image
        {
            get { return image; }
            set { image = value; }
        }
        public Tag Tag
        {
            get { return tag; }
            set { tag = value; }
        }
        public ProdProperties[] Properties
        {
            get { return properties; }
            set { properties = value; }
        }
        public int Priority
        {
            get { return priority; }
            set { priority = value; }
        }
        public DateTime PriorityEndDate
        {
            get { return priorityEndDate; }
            set { priorityEndDate = value; }
        }
        public decimal PriorityFinal
        {
            get { return priorityFinal; }
            set { priorityFinal = value; }
        }

        public Product[] Search(string searchString)
        {
            ArrayList productList = new ArrayList();
            Product ret = new Product();
            DataSet1.TBProductDataTable productRow = productTableAdapter.Search(searchString);
            foreach (DataSet1.TBProductRow product in productRow)
            {

                Enterprise enterprise = new MadeInPortugal.Enterprise();
                enterprise = enterprise.Get(product.Enterprise);

                ProductImage prodImage = new ProductImage();
                prodImage = prodImage.getImage(product.ID);

                ProdProperties prodProperties = new ProdProperties();
                ProdProperties[] properties = prodProperties.GetProperties(product.ID);

                Tag tag = new MadeInPortugal.Tag();
                tag = tag.GetTagByProductId(product.ID);

                productList.Add(new Product()
                {
                    Id = product.ID,
                    Enterprise = enterprise,
                    Price = product.Price,
                    ProductName = product.ProductName,
                    Specifications = product.Specifications,
                    Image = prodImage,
                    Properties = properties,
                    Tag = tag,
                    MinReq = product.MinReq,
                    Priority = product.Priority,
                    PriorityEndDate = product.PriorityDate,
                    priorityFinal = product.priorityCurrentNumber
                });
            }
            return (Product[])productList.ToArray(typeof (Product));
        }

        public Product GetById(int productId)
        {
            Product ret = new Product();
            DataSet1.TBProductDataTable productRow = productTableAdapter.GetData(productId);
            foreach (DataSet1.TBProductRow product in productRow)
            {

                Enterprise enterprise = new MadeInPortugal.Enterprise();
                enterprise = enterprise.Get(product.Enterprise);

                ProductImage prodImage = new ProductImage();
                prodImage = prodImage.getImage(product.ID);

                ProdProperties prodProperties = new ProdProperties();
                ProdProperties[] properties = prodProperties.GetProperties(product.ID);

                Tag tag = new MadeInPortugal.Tag();
                tag = tag.GetTagByProductId(product.ID);

                ret = new Product()
                {
                    Id = product.ID,
                    Enterprise = enterprise,
                    Price = product.Price,
                    ProductName = product.ProductName,
                    Specifications = product.Specifications,
                    Image = prodImage,
                    Properties = properties,
                    Tag = tag,
                    MinReq = product.MinReq,
                    Priority = product.Priority,
                    PriorityEndDate = product.PriorityDate,
                    priorityFinal = product.priorityCurrentNumber
                };
            }
            return ret;
        }
        public Product[] GetByEnterprise(int EnterpriseId)
        {

            ArrayList ret = new ArrayList();
            DataSet1.TBProductDataTable productRow = productTableAdapter.GetProductByEnterpriseId(EnterpriseId);
            foreach (DataSet1.TBProductRow product in productRow)
            {
                Enterprise enterprise = new MadeInPortugal.Enterprise();
                enterprise = enterprise.Get(product.Enterprise);

                ProductImage prodImage = new ProductImage();
                prodImage = prodImage.getImage(product.ID);

                ProdProperties prodProperties = new ProdProperties();
                ProdProperties[] properties = prodProperties.GetProperties(product.ID);

                ret.Add(new Product()
                {
                    Id = product.ID,
                    Enterprise = enterprise,
                    Price = product.Price,
                    ProductName = product.ProductName,
                    Specifications = product.Specifications,
                    Image = prodImage,
                    Properties = properties,
                    Tag = tag,
                    MinReq = product.MinReq,
                    Priority = product.Priority,
                    PriorityEndDate = product.PriorityDate,
                    priorityFinal = product.priorityCurrentNumber
                });

            }

            return (Product[])ret.ToArray(typeof(Product));

        }

        public Product[] GetByTagId(int tagId)
        {
            ArrayList ret = new ArrayList();
            Product prod = new Product();
            ProductTag prodIds = new ProductTag();
            ProductTag[] Ids = prodIds.GetProductId(tagId);
            foreach (ProductTag id in Ids)
            {

                DataSet1.TBProductDataTable productRow = productTableAdapter.GetData(id.ProductId);
                foreach (DataSet1.TBProductRow product in productRow)
                {
                    Enterprise enterprise = new MadeInPortugal.Enterprise();
                    enterprise = enterprise.Get(product.Enterprise);

                    ProductImage prodImage = new ProductImage();
                    prodImage = prodImage.getImage(product.ID);

                    ProdProperties prodProperties = new ProdProperties();
                    ProdProperties[] properties = prodProperties.GetProperties(product.ID);

                    ret.Add(new Product()
                    {
                        Id = product.ID,
                        Enterprise = enterprise,
                        Price = product.Price,
                        ProductName = product.ProductName,
                        Specifications = product.Specifications,
                        Image = prodImage,
                        Properties = properties,
                        Tag = tag,
                        MinReq = product.MinReq,
                        Priority = product.Priority,
                        PriorityEndDate = product.PriorityDate,
                        priorityFinal = product.priorityCurrentNumber
                    }

                        );
                }
            }



            return (Product[])ret.ToArray(typeof(Product));
        }
        //public Product insert(string name,Enterprise enterprise,decimal price,string specification, int minimumreq,ProductImage image,Tag[] tags)
        public Product insert(Product product)
        {

            short? productId = 0;
            int prodId;
            productTableAdapter.Insert1(product.ProductName,
                product.enterprise.Id,
                (decimal?)product.price,
                product.Specifications,
                product.MinReq, product.priority,
                product.priorityEndDate,
                product.PriorityFinal,
                out productId);

            int.TryParse(productId.ToString(), out prodId);
            product.Id = prodId;
            product.Image.Id = prodId;
            ProductTag prodTag = new ProductTag();

            prodTag.Insert(tag.ID, (int)product.Id);
            tag.Insert(tag.TagName);


            ProdProperties proPerties = new ProdProperties();
            proPerties.InsertProperties(product.Properties, product.Id);
            if (product.Image != null)
            {
                product.Image.Id = product.id;
                ProductImage image = new ProductImage();
                image.insertImage(product.Image.Image, product.Image.Description, product.Image.Id);
            }
            return product;
        }

        public Product update(Product product)
        {


            productTableAdapter.UpdateItem(product.ProductName, product.enterprise.Id, (decimal)product.price, product.Specifications, product.MinReq, product.Id, product.Id);

            ProductTag prodTag = new ProductTag();
            prodTag.Delete(product.Id);

            prodTag.Insert(tag.ID, (int)product.Id);


            ProdProperties proPerties = new ProdProperties();
            proPerties.InsertProperties(product.Properties, product.Id);
            if (product.Image != null)
            {
                product.Image.Id = product.Id;
                ProductImage image = new ProductImage();
                try
                {
                    image.deleteImage(product.Image.Id);
                    image.insertImage(product.Image.Image, product.Image.Description, product.Image.Id);
                }
                catch (Exception)
                {

                    //image.updateImage(product.Image.Image, product.Image.Description, product.Image.Id);

                }

            }

            return product;
        }

        public void delete(int productId)
        {

            ProdProperties proPerties = new ProdProperties();
            proPerties.DeleteProperties(productId);



            productTableAdapter.Delete(productId);
            TBProductTagTableAdapter ProductTagTableAdapter = new TBProductTagTableAdapter();
            //TBTagTableAdapter TagTableAdapter = new TBTagTableAdapter();
            Tag tag = new Tag();
            TagType tagtype = new TagType();
            DataSet1.TBProductTagDataTable tagIds = ProductTagTableAdapter.GetData(productId);
            foreach (DataSet1.TBProductTagRow tagrow in tagIds)
            {
                try
                {
                    tag.Delete(tagrow.TagID);
                    tagtype.Delete(tagrow.TagID);
                }
                catch (Exception)
                {
                }

            }
            ProductTag prodTag = new ProductTag();
            prodTag.Delete(productId);

            return;
        }
    }


    public class ProductImage
    {
        int id;
        byte[] image;
        string description;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public byte[] Image
        {
            get { return image; }
            set { image = value; }
        }
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public ProductImage getImage(int ImageId)
        {
            ProductImage ret = new ProductImage();
            TBImageTableAdapter imageTableAdapter = new TBImageTableAdapter();
            DataSet1.TBImageDataTable imageTable = imageTableAdapter.GetData(ImageId);
            foreach (DataSet1.TBImageRow imageRow in imageTable)
            {

                ret.Id = imageRow.ID;
                ret.Image = imageRow.Image;
                ret.Description = imageRow.Description;
            }
            return ret;

        }

        public ProductImage insertImage(byte[] image, string description, int ImageId)
        {
            TBImageTableAdapter imageTableAdapter = new TBImageTableAdapter();
            imageTableAdapter.Insert(image, description, ImageId);
            return new ProductImage() { Image = image, Description = description, Id = ImageId };

        }

        public ProductImage updateImage(byte[] image, string description, int ImageId)
        {
            TBImageTableAdapter imageTableAdapter = new TBImageTableAdapter();
            imageTableAdapter.Update(image, description, ImageId);
            return new ProductImage() { Image = image, Description = description, Id = ImageId };
        }

        public bool deleteImage(int ImageId)
        {
            bool ret = false;
            TBImageTableAdapter imageTableAdapter = new TBImageTableAdapter();
            try
            {
                imageTableAdapter.Delete(ImageId);
            }
            catch (Exception)
            {

                ret = true;
            }

            return ret;

        }
    }
}
